# -*- coding: utf-8 -*-
import pandas as pd
import numpy as np
import time
import huber 
from forecast_models import *
from accuracy import RMSE,MAE,MAPE,sMAPE
df = pd.read_csv('trainSunData.csv',index_col = 0)
df.fillna(method='pad', inplace=True)
import warnings
warnings.filterwarnings('ignore')
horizon = 1
sequence_length = 8
index = 'Power'
# In[]
# 存储过程

times = time.strftime('%Y-%m-%d-%H-%M',time.localtime(time.time()))
savepath = '预测结果保存'+'/'
filename = str(times)+ "预测值.csv"
generpath(savepath)
#%% SVR模型
model_index1 = 'SVR'
y_test_rel,y_svr_pre = svr_model(df,sequence_length = sequence_length,horizon = horizon,index=index)
mae_svr = MAE(y_test_rel,y_svr_pre)
rmse_svr = RMSE(y_test_rel,y_svr_pre)
mape_svr = sMAPE(y_test_rel,y_svr_pre)
print('svr的MAE为：'+str(mae_svr)) #"MAE:"
print('svr的RMSE为：'+str(rmse_svr)) #"RMSE:"
print('svr的MAPE为：'+str(mape_svr))

# 存储预测值与真实值
generfile(savepath,filename,len(y_test_rel))
datasave(savepath+filename,'real_data',y_test_rel)
datasave(savepath+filename,model_index1,y_svr_pre)
# In[56]:
#真实值与预测值折线图
plt.plot(y_svr_pre, label=model_index1)
plt.plot(y_test_rel, label='real_data')
plt.xlabel('No. of Trading Numbers')
plt.ylabel('Values')
plt.legend(loc='upper right')
fig = plt.gcf()
fig.set_size_inches(15, 5)
fig.savefig(savepath+str(times)+model_index1+'真实值与预测值比较.png', dpi=300)
plt.show()
#%% BP模型
model_index2 = 'BPNN'
y_test_rel,y_bpnn_pre = bpnn_model(df,sequence_length = sequence_length,horizon = horizon,index=index, epochs = 25)
#y_bpnn_pre = y_bpnn_pre.astype('float64')
mae_bpnn = MAE(y_test_rel,y_bpnn_pre)
rmse_bpnn = RMSE(y_test_rel,y_bpnn_pre)
mape_bpnn = sMAPE(y_test_rel,y_bpnn_pre)
print('bpnn的MAE为：'+str(mae_bpnn)) #"MAE:"
print('bpnn的RMSE为：'+str(rmse_bpnn)) #"RMSE:"
print('bpnn的MAPE为：'+str(mape_bpnn))

# 存储预测值与真实值
generfile(savepath,filename,len(y_test_rel))
datasave(savepath+filename,'real_data',y_test_rel)
datasave(savepath+filename,model_index2,y_svr_pre)
# In[56]:
#真实值与预测值折线图
plt.plot(y_bpnn_pre, label=model_index2)
plt.plot(y_test_rel, label='real_data')
plt.xlabel('No. of Trading Numbers')
plt.ylabel('Values')
plt.legend(loc='upper right') 
fig = plt.gcf()
fig.set_size_inches(15, 5)
fig.savefig(savepath+str(times)+model_index2+'真实值与预测值比较.png', dpi=300)
plt.show()
#%% ELM模型
model_index3 = 'ELM'
y_test_rel,y_elm_pre = elm_model(df,sequence_length = sequence_length,horizon = horizon,index=index)
mae_elm = MAE(y_test_rel,y_elm_pre)
rmse_elm = RMSE(y_test_rel,y_elm_pre)
mape_elm = sMAPE(y_test_rel,y_elm_pre)
print('elm的MAE为：'+str(mae_elm)) #"MAE:"
print('elm的RMSE为：'+str(rmse_elm)) #"RMSE:"
print('elm的MAPE为：'+str(mape_elm))

# 存储预测值与真实值
generfile(savepath,filename,len(y_test_rel))
datasave(savepath+filename,'real_data',y_test_rel)
datasave(savepath+filename,model_index3,y_svr_pre)
# In[56]:
#真实值与预测值折线图
plt.plot(y_elm_pre, label=model_index3)
plt.plot(y_test_rel, label='real_data')
plt.xlabel('No. of Trading Numbers')
plt.ylabel('Values')
plt.legend(loc='upper right')
fig = plt.gcf()
fig.set_size_inches(15, 5)
fig.savefig(savepath+str(times)+model_index3+'真实值与预测值比较.png', dpi=300)
plt.show()
#%% LSTM
model_index4 = 'LSTM'
y_test_rel,y_lstm_pre = lstm_model(df,sequence_length = sequence_length,horizon = horizon,index =index, epochs = 10)
mae_lstm = MAE(y_test_rel,y_lstm_pre)
rmse_lstm = RMSE(y_test_rel,y_lstm_pre)
mape_lstm = sMAPE(y_test_rel,y_lstm_pre)
print('lstm的MAE为：'+str(mae_lstm)) #"MAE:"
print('lstm的RMSE为：'+str(rmse_lstm)) #"RMSE:"
print('lstm的MAPE为：'+str(mape_lstm))

# 存储预测值与真实值
generfile(savepath,filename,len(y_test_rel))
datasave(savepath+filename,'real_data',y_test_rel)
datasave(savepath+filename,model_index1,y_svr_pre)
# In[56]:
#真实值与预测值折线图
plt.plot(y_lstm_pre, label=model_index4)
plt.plot(y_test_rel, label='real_data')
plt.xlabel('No. of Trading Numbers')
plt.ylabel('Values')
plt.legend(loc='upper right')
fig = plt.gcf()
fig.set_size_inches(15, 5)
fig.savefig(savepath+str(times)+model_index4+'真实值与预测值比较.png', dpi=300)
plt.show()

#%% 预测数据保存
y_total = np.hstack((y_test_rel,y_lstm_pre))
y_total = np.hstack((y_total,y_elm_pre))
y_total = np.hstack((y_total,y_bpnn_pre))
y_total = np.hstack((y_total,y_svr_pre))
y_t = pd.DataFrame(y_total)
y_t.columns=['y_true','y_lstm_pre','y_elm_pre','y_bpnn_pre','y_svr_pre']
ttx = '太阳山bench预测结果.csv'
y_t.to_csv(ttx)
#%% 误差数据保存
ind1 = np.array([mae_lstm ,rmse_lstm ,mape_lstm]).reshape(-1,1)
ind2 = np.array([mae_elm ,rmse_elm ,mape_elm]).reshape(-1,1)
ind3 = np.array([mae_bpnn ,rmse_bpnn ,mape_bpnn]).reshape(-1,1)
ind4 = np.array([mae_svr ,rmse_svr ,mape_svr]).reshape(-1,1)

ind = np.hstack((ind1,ind2))
ind = np.hstack((ind,ind3))
ind = np.hstack((ind,ind4))
indx = pd.DataFrame(ind)
indx.columns=['LSTM','elm','bpnn','svr']
indx.index = ['MAE','RMSE','MAPE']
tt = '太阳山bench误差指标.csv'
indx.to_csv(tt)








